<?php

namespace App\Http\Controllers\Admin;

use App\Models\AdminUsers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\TeacherGroups;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller
{
    public function index(){
        $adminusers = AdminUsers::paginate(config('constants.page_num'));
        return view('admin.admin.index', compact('adminusers'));
    }

    public function show($id){
        $admin = AdminUsers::with('roles.perms')->find($id);
        $roles = Role::get();
        return view('admin.admin.show', compact('admin', 'roles'));
    }

    public function create(){
        $groups = TeacherGroups::get();
        return view('admin.admin.create', compact('groups'));
    }

    public function store(Requests\CreateAdminUsersRequest $request){
        $data = $request->all();
        $data['password'] = bcrypt($data['password']);
        $data['login_ip'] = $request->getClientIp();
        $result = AdminUsers::create($data);
        if ($result){
            exit(json_encode(array('code'=>1, 'url'=>'/admin/adminuser')));
        }else{
            exit(json_encode(array('code'=>0, 'msg'=>$result)));
        }
    }

    public function edit($id){
        $admin = AdminUsers::findOrFail($id);
        return view('admin.admin.edit', compact('admin'));
    }

    public function update(Requests\CreateAdminUsersRequest $request, $id){
        $admin = AdminUsers::findOrFail($id);
        $data = $request->all();
        unset($data['username']);
        if ($data['password'] == null){
            unset($data['password']);
        }else{
            $data['password'] = bcrypt($data['password']);
        }
        $result = $admin->update($data);
        if ($result){
            exit(json_encode(array('code'=>1, 'url'=>'/admin/adminuser')));
        }else{
            exit(json_encode(array('code'=>0, 'msg'=>$result)));
        }
    }

    public function destroy($id){
        $admin = AdminUsers::findOrFail($id);
        if ($admin){
            $admin->delete();
            exit(json_encode(array('code'=>1)));
        } else {
            exit(json_encode(array('code'=>0)));
        }
    }

    public function roleAdd(Request $request, $id){
        $admin = AdminUsers::findOrFail($id);
        if ($roleArr = $request->role){
            $admin->roles()->sync($roleArr);
        }else{
            $admin-roles()->detach();
        }
        exit(json_encode(['code'=>1, 'url'=>'/admin/adminuser/'.$id]));
    }

    public function getLogs(Request $request){
        if ($request->isMethod('post')){
            $logs = DB::table('admin_logs')
                ->join('admin_users','admin_logs.admin_id','=','admin_users.id')
                ->where('username', 'like', '%'.$request->table_search.'%')
                ->orwhere('log_info', 'like', '%'.$request->table_search.'%')
                ->latest('log_time')
                ->paginate(config('constants.page_num'));
        }else{
            $logs = DB::table('admin_logs')
                ->join('admin_users','admin_logs.admin_id','=','admin_users.id')
                ->latest('log_time')
                ->paginate(config('constants.page_num'));

        }
        return view('admin.admin.logs', compact('logs'));
    }

}
